<?php
/**
 * Created by PhpStorm.
 * User: yangyou
 * Date: 2018/12/3 0003
 * Time: 09:21
 */

namespace app\index\controller;


use app\index\model\Customized;
use app\index\model\Substation;
use app\index\model\User;

class privateorder extends Base
{
    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:私人订制
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        return view('privateorder/order');
    }

    /**
     * Author:yang you
     * Time:2018-11-15 10:00:43
     * Describe:私人订制订单
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function addOrder()
    {
        $tel = input("tel");
        $area = input("area");
        $floor = input("buildingType");
        $name = input("name");
        if(empty($name)){
            $name = $tel;//没传姓名，电话号码做用户名
        }
        //查询该电话号码是否存在不存在就注册。
        if(preg_match_all("/^1[34578]\d{9}$/", $tel)){
            $user = new User();
            $customized = new Customized();

            $user_info = $user->checkName($tel);
            if($user_info == null){
                $substation=new Substation();
                $substation_id=$substation->getSubId(session('host_name'));
                $user_data = ['tel'=>$tel,'create_ip'=>request()->ip(),'nick_name'=>$name,'substation_id'=>$substation_id,'substation_name'=>session('host_name')];
                $user->mySave($user_data,"user_id");//注册账号
                $user_info = $user->checkName($tel);
                $data = ['tel'=>$tel,'create_time'=>time(),'area'=>$area,'floor'=>$floor,'user_id'=>$user_info['user_id'],'substation_id'=>$substation_id,'substation_name'=>session('host_name')];
                $order = $customized->mySave($data,"customized_id");
                if($order){
                    return json("ok");
                }else{
                    return json("订单提交失败");
                }
            }else{
                $data = ['tel'=>$tel,'create_time'=>time(),'area'=>$area,'floor'=>$floor,'user_id'=>$user_info['user_id']];
                $order = $customized->mySave($data,"customized_id");
                if($order){
                    return json("ok");
                }else{
                    return json("订单提交失败");
                }
            }
        }else{
            return json("填写的手机号码有错误");
        }
    }

}